package com.lanca.controller;

import com.lanca.dto.DbServiceKitVo;
import com.lanca.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@RestController
public class UserController {


    private static final Logger log = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private RestTemplate restTemplate;

//    @Autowired
//    private ProductClient productClient;


    @PostMapping("/invoke")
    public ResultVo invokeProduct() {
        //String product = productClient.product(22);
        log.info("调用用户服务成功");
        //设置Http的Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        String url = "http://localhost:8992/product";
        //设置访问参数
        HashMap<String, Object> params = new HashMap<>();
        params.put("name", "anyway");

        //设置访问的Entity
        HttpEntity entity = new HttpEntity(params, headers);

        // 下面方法可行
        ParameterizedTypeReference<ResultVo<DbServiceKitVo>> reference = new ParameterizedTypeReference<ResultVo<DbServiceKitVo>>() {};
        ResponseEntity<ResultVo<DbServiceKitVo>> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, reference);

        // spring-web:5.2.44.RELEASE

//        ResultVo<DbServiceKitVo> resultVo = new ResultVo<>();
//        ResponseEntity<? extends ResultVo> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, resultVo.getClass());
        return exchange.getBody();
    }
}
